package com.jerry.android.ListView.BaseAdapterZhy.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.jerry.android.ListView.BaseAdapterZhy.adapter.ListViewCommonAdapter;
import com.jerry.android.R;
import com.jerry.android.model.ListInfoBean;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.List;

public class BaseAdapterZhyActivity extends Activity {

    private Context mContext;
    private ListView mListView;
    private ListViewCommonAdapter<ListInfoBean> mCommonAdapter;
    private List<ListInfoBean> mDataList = new ArrayList<ListInfoBean>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview_base_adapter);
        mContext = BaseAdapterZhyActivity.this;

        initView();
        initData();
        initListener();
    }

    private void initView(){
        mListView = (ListView) findViewById(R.id.list_view);
    }

    private void initData(){

        mDataList = generateData();
        mCommonAdapter = new ListViewCommonAdapter<ListInfoBean>(getApplicationContext(),R.layout.item_listview_base_adapter,mDataList) {
            @Override
            protected void convert(ViewHolder viewHolder, ListInfoBean item, int position) {
                viewHolder.setText(R.id.title, item.getTitle());
                viewHolder.setText(R.id.sub_title, item.getSubTitle());
            }
        };
        mListView.setAdapter(mCommonAdapter);

    }

    private void initListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long rowId) {
                ListInfoBean infoBean = mDataList.get(position);
                Toast.makeText(mContext, "你点击了"+infoBean.getSubTitle(),Toast.LENGTH_SHORT).show();
            }
        });
    }

    private List<ListInfoBean> generateData() {
        ListInfoBean tabhost = new ListInfoBean();
        tabhost.setTitle("aaa");
        tabhost.setSubTitle("aaaaa");
        mDataList.add(tabhost);

        ListInfoBean viewpager = new ListInfoBean();
        viewpager.setTitle("bbb");
        viewpager.setSubTitle("bbbbb");
        mDataList.add(viewpager);

        ListInfoBean fragment = new ListInfoBean();
        fragment.setTitle("ccc");
        fragment.setSubTitle("ccccc");
        mDataList.add(fragment);

        ListInfoBean fpa = new ListInfoBean();
        fpa.setTitle("ddd");
        fpa.setSubTitle("ddddd");
        mDataList.add(fpa);

        return mDataList;
    }

}
